﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControlSupport_NewsProducts : System.Web.UI.UserControl
{
    DataTable dataTable;
    protected void Page_Load(object sender, EventArgs e)
    {
        dataTable = new DataTable();
        dataTable.Columns.Add("ProImage");
        dataTable.Columns.Add("ProName");
        dataTable.Columns.Add("usefor");
        dataTable.Columns.Add("gender");
        dataTable.Columns.Add("view");
        dataTable.Columns.Add("ProId");

        string catalogy = Request["catalogy"];
        string id = Request["id"];

        switch (catalogy)
        {
            case "type": ShowType(id); break;
            case "usefor": ShowUsefor(id); break;
            case "gender": ShowGender(id); break;
            case "new": ShowNew();  break;
            case "mostview": ShowMostView(); break;
            default: ShowMostView(); break;
        }
        
        
        CollectionPager1.DataSource = dataTable.DefaultView;
        CollectionPager1.BindToControl = ddlShowProducts;
        ddlShowProducts.DataSource = CollectionPager1.DataSourcePaged;
    }

    protected void ShowType(String id)
    {
        ShoppingDataContext spct = new ShoppingDataContext();
        var query = from p in spct.PRODUCTs 
                    where p.type == id
                    select p;
        foreach (var c in query)
        {
            DataRow row = dataTable.NewRow();
            row["ProImage"] = c.image;
            row["ProName"] = c.ProName;
            row["usefor"] = c.usefor;
            row["gender"] = c.gender;
            row["view"] = c.countview;
            row["ProId"] = c.ProductID;
            dataTable.Rows.Add(row);
        }
    }

    protected void ShowUsefor(String id)
    {
        ShoppingDataContext spct = new ShoppingDataContext();
        var query = from p in spct.PRODUCTs
                    where p.usefor == id
                    select p;
        foreach (var c in query)
        {
            DataRow row = dataTable.NewRow();
            row["ProImage"] = c.image;
            row["ProName"] = c.ProName;
            row["usefor"] = c.usefor;
            row["gender"] = c.gender;
            row["view"] = c.countview;
            row["ProId"] = c.ProductID;
            dataTable.Rows.Add(row);
        }
    }

    protected void ShowGender(String id)
    {
        ShoppingDataContext spct = new ShoppingDataContext();
        var query = from p in spct.PRODUCTs
                    where p.gender == id
                    select p;
        foreach (var c in query)
        {
            DataRow row = dataTable.NewRow();
            row["ProImage"] = c.image;
            row["ProName"] = c.ProName;
            row["usefor"] = c.usefor;
            row["gender"] = c.gender;
            row["view"] = c.countview;
            row["ProId"] = c.ProductID;
            dataTable.Rows.Add(row);
        }
    }

    protected void ShowNew()
    {
        ShoppingDataContext spct = new ShoppingDataContext();
        var query = from p in spct.PRO_IMPORTs
                    join q in spct.PRODUCTs
                    on p.ProductID equals q.ProductID
                    orderby p.importdate descending
                    select q;
        foreach (var c in query)
        {
            DataRow row = dataTable.NewRow();
            row["ProImage"] = c.image;
            row["ProName"] = c.ProName;
            row["usefor"] = c.usefor;
            row["gender"] = c.gender;
            row["view"] = c.countview;
            row["ProId"] = c.ProductID;
            dataTable.Rows.Add(row);
        }
    }

    protected void ShowMostView()
    {
        ShoppingDataContext spct = new ShoppingDataContext();
        var query = from p in spct.PRODUCTs
                    orderby p.countview descending
                    select p;
        foreach (var c in query)
        {
            DataRow row = dataTable.NewRow();
            row["ProImage"] = c.image;
            row["ProName"] = c.ProName;
            row["usefor"] = c.usefor;
            row["gender"] = c.gender;
            row["view"] = c.countview;
            row["ProId"] = c.ProductID;
            dataTable.Rows.Add(row);
        }
    }

    protected void ddlShowProducts_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        HyperLink hyper = (HyperLink)e.Item.FindControl("_ProName");
        HiddenField hdfId=(HiddenField)e.Item.FindControl("_ProId");
        hyper.NavigateUrl = String.Format("~/ProductDetail.aspx?id={0}", hdfId.Value);
    }
}
